package cn.wit.testalgorithms;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试两数之和
 * 
 * @author 16604
 *
 */
public class DemoTwoNumbersAdd {
	public static void main(String[] args) {

		int[] num = { 2, 7, 11, 15 };
		int target = 9;
		twoSum(num, target);
		// System.out.println(target);
	}

	public static int[] twoSum(int[] nums, int target) {
		// 定义一个HashMap
		Map<Integer, Integer> map = new HashMap<>();
		// 接下来把数组里面得元素，下标，对应存在在Map里面去
		for (int i = 0; i < nums.length; i++) {
			map.put(nums[i], i);
		}
		// 去比较呢，
		for (int j = 0; j < nums.length; j++) {
			int complent = target - nums[j];
			System.out.println(map.get(complent));
			System.out.println(map.containsKey(complent));
			if (map.containsKey(complent) && map.get(complent) != j) {
				return new int[] { j, map.get(complent) };
			}
		}
		throw new IllegalArgumentException("No two sum solution");
	}
}
